package com.bill.gaty.mfidelity.activities;

 
import java.util.ArrayList;
 
import java.util.List;
 
 
import com.bill.gaty.mfidelity.R;
import com.bill.gaty.mfidelity.beans.Gtf;
import com.bill.gaty.mfidelity.dao.impl.SecurityDaoImpl;
import com.bill.gaty.mfidelity.dto.HistoriqueGtfRestaurationResponse;
import com.bill.gaty.mfidelity.dto.ResponseData;

import com.bill.gaty.mfidelity.webservices.WebService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

 
 
import android.app.Activity;
import android.content.Intent;
 
import android.os.Bundle;
import android.util.Log;
 
public class LookUpGtfHistoryActivity extends  Activity {
private String password,
               username;
private int  offset,maxresult;
private List<Gtf> info;
protected String responseMessage;
private int responseCode;
private SecurityDaoImpl sd;

	@Override
	public void finish() {
		// TODO Auto-generated method stub
		String data=new Gson().toJson(info,       new TypeToken<List<Gtf>>(){}.getType());
		Intent intent=new Intent();
		Bundle b=new Bundle();
		 b.putString("data", data);
		 b.putString("responseMessage", responseMessage);
		 b.putInt("nextOffset", offset);
		 b.putInt("maxresult", maxresult);
		 b.putInt("responseCode", responseCode);
		 
		 intent.putExtras(b);
		 
    	setResult(RESULT_OK, intent);
		super.finish();
		 
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.compatibilite_activity_layout);
		Bundle b=getIntent().getExtras();
		username=b.getString("username");
		password=b.getString("password");
		offset=b.getInt("offset",0);
		maxresult=b.getInt("maxresult",20);
		
		info=new ArrayList<Gtf>();
		Thread t=new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try{
					
		            
					HistoriqueGtfRestaurationResponse data = WebService.retriveGtf(username, password, offset, maxresult);
//					Gson g=new Gson();
//					 java.lang.reflect.Type collectionType = new TypeToken<List<Gtf>>(){}.getType();
//				      
					sd=new SecurityDaoImpl(getApplicationContext());
					sd.open();
					maxresult=data.getMaxresult();
					offset=data.getNextoffset();
					responseCode=data.getResponseCode();
					LookUpGtfHistoryActivity.this.responseMessage=data.getResponseMessage();
					List<ResponseData> responseData = data.getResponseData();
					Log.e("List<ResponseData>",responseData.toString());
					for (ResponseData responseData2 : responseData) {
						try{
						Gtf gtf=responseData2.getGtf();
						Log.e("Gtf gtf=responseData2.getGtf();",gtf+"");
						gtf=sd.getUnSecureGtfFromSecureGtf(gtf);
						 info.add(gtf);
						}catch (Exception e) {
							// TODO: handle exception
							Log.e("Exception.for (ResponseData responseData2 : responseData)",e.getMessage()+"");
						}
					}
					Log.i(" Lookup.info",info+"");
					try{
					sd.close();
					}catch (Exception e) {
						// TODO: handle exception
						Log.e("Exception.sd.close();",e.getMessage()+"");
					}
					 
					    
					   
				}catch (Exception e) {
					// TODO: handle exception
					Log.i("Exception.Thread t",e.getMessage()+"");
					sd.close();
				}
				finish();
			}
		 
	});
		t.start();
	}

}
